package com.sanqing.dao;

import java.util.Date;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Restrictions;

import com.sanqing.po.Candidate;

/**
 * A data access object (DAO) providing persistence and search support for
 * Candidate entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.sanqing.po.Candidate
 * @author MyEclipse Persistence Tools
 */

public class CandidateDao extends BaseHibernateDao {
	private static final Log log = LogFactory.getLog(CandidateDao.class);
	// property constants
	public static final String NAME = "name";
	public static final String SEX = "sex";
	public static final String POSITION = "position";
	public static final String SPECIALTY = "specialty";
	public static final String EXPERIENCE = "experience";
	public static final String EDUCATION = "education";
	public static final String SCHOOL = "school";
	public static final String PHONE = "phone";
	public static final String EMAIL = "email";
	public static final String CONTENT = "content";
	public static final String ISSTOCK = "isstock";

	public void save(Candidate transientInstance) {
		log.debug("saving Candidate instance");
		try {
			Session session = getSession();
			Transaction tx = session.beginTransaction();
			session.save(transientInstance);
			tx.commit();
			closeSession();
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Candidate persistentInstance) {
		log.debug("deleting Candidate instance");
		try {
			Session session = getSession();
			Transaction tx = session.beginTransaction();
			session.delete(persistentInstance);
			tx.commit();
			closeSession();
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}
	
	public void update(Candidate persistentInstance) {
		log.debug("deleting Candidate instance");
		try {
			Session session = getSession();
			Transaction tx = session.beginTransaction();
			session.update(persistentInstance);
			tx.commit();
			closeSession();
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Candidate findById(java.lang.Integer id) {
		log.debug("getting Candidate instance with id: " + id);
		try {
			Candidate instance = (Candidate) getSession().get(
					"com.sanqing.po.Candidate", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Candidate instance) {
		log.debug("finding Candidate instance by example");
		try {
			List results = getSession().createCriteria(
					"com.sanqing.po.Candidate").add(Example.create(instance))
					.list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Candidate instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Candidate as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByName(Object name) {
		return findByProperty(NAME, name);
	}

	public List findBySex(Object sex) {
		return findByProperty(SEX, sex);
	}

	public List findByPosition(Object position) {
		return findByProperty(POSITION, position);
	}

	public List findBySpecialty(Object specialty) {
		return findByProperty(SPECIALTY, specialty);
	}

	public List findByExperience(Object experience) {
		return findByProperty(EXPERIENCE, experience);
	}

	public List findByEducation(Object education) {
		return findByProperty(EDUCATION, education);
	}

	public List findBySchool(Object school) {
		return findByProperty(SCHOOL, school);
	}

	public List findByPhone(Object phone) {
		return findByProperty(PHONE, phone);
	}

	public List findByEmail(Object email) {
		return findByProperty(EMAIL, email);
	}

	public List findByContent(Object content) {
		return findByProperty(CONTENT, content);
	}

	public List findByIsstock(Object isstock) {
		return findByProperty(ISSTOCK, isstock);
	}

	public List findAll() {
		log.debug("finding all Candidate instances");
		try {
			String queryString = "from Candidate";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	public List search(Integer id, String name, Integer sex, Integer deptId, 
			Integer positionId, String specialty, String education, 
			String experience, String school, Boolean isstock, Integer ishired) {
		Criteria criteria = getSession().createCriteria("com.sanqing.po.Candidate");
		if(id!=null&&id>0){
			criteria.add(Restrictions.eq("id", id));
		}
		if(name!=null&&!name.equals("")){
			criteria.add(Restrictions.like("name", "%"+name+"%"));
		}
		if(sex!=null&&sex>=0){
			criteria.add(Restrictions.eq("sex", sex==1?true:false));
		}
		if(deptId!=null&&deptId>0){
			criteria.createAlias("department", "dept")
			.add(Restrictions.eq("dept.id", deptId));
		}
		if(positionId!=null&&positionId>0){
			criteria.createAlias("position", "position")
			.add(Restrictions.eq("position.id", positionId));
		}
		if(specialty!=null&&!specialty.equals("")){
			criteria.add(Restrictions.like("specialty", "%"+specialty+"%"));
		}
		if(education!=null&&!education.equals("")){
			criteria.add(Restrictions.like("education", "%"+education+"%"));
		}
		if(experience!=null&&!experience.equals("")){
			criteria.add(Restrictions.like("experience", "%"+experience+"%"));
		}
		if(school!=null&&!school.equals("")){
			criteria.add(Restrictions.like("school", "%"+school+"%"));
		}
		if(isstock!=null){
			criteria.add(Restrictions.eq("isstock", isstock));
		}
		if(ishired!=null&&ishired>=0){
			criteria.add(Restrictions.eq("ishired", ishired==1?true:false));
		}
		List results = criteria.list();
		return results;
	}

	public Candidate merge(Candidate detachedInstance) {
		log.debug("merging Candidate instance");
		try {
			Candidate result = (Candidate) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Candidate instance) {
		log.debug("attaching dirty Candidate instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Candidate instance) {
		log.debug("attaching clean Candidate instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}